<?php
namespace Home\Controller;
/**
 * 我的域名
 * @author Administrator
 *
 */
use Org\Util\Sms;
class MydomainController extends HomeBaseController
{
    public function index(){
       
        $page=I('page',1);
        $size = I('ps',30);
        if($size==''){
            $size=30;
        }
        $this->assign('ps', $size);
        $start=($page-1)*$size;
        $condstr='';
        $condtion['uid']=session('uid');
        $condtion['check_status']=3;
        $domType=I('domType','');
        $this->assign('domType', $domType);      
        if($domType!=''){
            $condstr=$condstr.' and a.domain_typeid='.$domType;
            $condtion['domain_typeid']=$domType;
        }
       $expiredate=I('expiredate','');
       if($expiredate==4 || $expiredate=='4'){
           $condtion['expriedate'] = array('egt',date("Y-m-d",strtotime("90 day")).' 00:00:00');
           $condstr=$condstr." and a.expriedate >='".date("Y-m-d",strtotime("90 day"))." 00:00:00'";
           
       }else if($expiredate!=''){
           $dayd=date("Y-m-d",strtotime(($expiredate*30)." day")).' 23:59:59';
           $condtion['expriedate'] = array(array('egt',date("Y-m-d").' 00:00:00'),array('elt',$dayd));
           $condstr=$condstr." and a.expriedate >='".date("Y-m-d")." 00:00:00' and a.expriedate<='".$dayd."'";
       }
       $this->assign('expiredate', $expiredate);
        $domain=I('domain','');
        $this->assign('domain', $domain);
        if($domain!=''){
            $condstr=$condstr." and a.domain like '%".$domain."'";
            $condtion['domain']=array("like", "%".$domain);
        }
        $domain_registrar=I('domain_registrar','');
        $this->assign('domain_registrar', $domain_registrar);
        if($domain_registrar!=''){
            $condstr=$condstr.' and a.reg_busin_id ='.$domain_registrar;
            $condtion['reg_busin_id']=$domain_registrar;
        }
        $is_loan=I('is_loan','');
        $this->assign('is_loan', $is_loan);
        if($is_loan!=''){
            $condstr=$condstr.' and a.is_lend ='.$is_loan;
            $condtion['is_lend']=$is_loan;
        }
        $trans_status=I('trans_status','');
        $this->assign('trans_status', $trans_status);
        if($trans_status!=''){
            $condstr=$condstr.' and a.trans_status ='.$trans_status;
            $condtion['trans_status']=$trans_status;
        }
        
        
        $DomainType = M('DomainType');
        $domTypes = $DomainType->select();
        $this->assign('domTypes', $domTypes);
        
        
        $Regbusiness = M('RegisterBusiness');
        $regblist=$Regbusiness->select();
        $this->assign('regblist', $regblist);
        
        $sql='SELECT a.id,a.uid,b.id as pushid,a.domain,a.domain_typeid,a.trans_status,a.price,a.is_mortage,a.expriedate,b.name ,c.name AS tyname FROM dd_user_domains a LEFT JOIN dd_register_business b ON a.reg_busin_id=b.id LEFT JOIN dd_domain_type c ON a.domain_typeid=c.id WHERE a.uid='.session("uid").$condstr.' AND a.check_status=3 ORDER BY a.createdate DESC LIMIT '.$start.','.$size;
        $dao= M('UserDomains');
        $domainList = $dao->query($sql);
        $allcount=$dao->where($condtion)->count();
        $this->assign('domainList', $domainList);
        $this->assign('zccount', $dao->where('uid = '.session("uid").' and trans_status=1')->count());
        $this->assign('jycount', $dao->where('uid = '.session("uid").' and trans_status=2')->count());
        $this->assign('dhcount', $dao->where('uid = '.session("uid").' and trans_status=3')->count());
        $this->assign('dycount', $dao->where('uid = '.session("uid").' and is_mortage=2')->count());
        $this->assign('allcount', $allcount);
        $this->assign('page', $page);
        $this->assign('pageNum', $allcount%$size);
        
        $this->display('mydomain/index');
    }
    
    public function mortgage(){
        $DomainType = M('DomainType');
        $domTypes = $DomainType->field(array("id","name","credit_limit"))->where("is_entrust=1")->select();
        $this->assign('domTypes', $domTypes);
        $this->assign('domTypesjson', json_encode($domTypes));
        $UserDomains = M('UserDomains');
        $domainList = $UserDomains->query("SELECT a.id,a.domain,a.price,a.trans_status,a.entrust,a.createdate,b.name FROM dd_user_domains a LEFT JOIN dd_domain_type b ON a.domain_typeid=b.id WHERE a.uid=".session('uid')." AND a.is_mortage=2");
        $this->assign('domainList', $domainList);
        $this->display('mydomain/mortgage');
    }
    
    public function fee(){
        $dotyId = I('category',0);
        $mortgage_period=I('mortgage_period',3);//抵押天数
        $mortgage_number=I('mortgage_number',1);//抵押数量
        $credilimit = M('DomainType')->field('credit_limit')->find($dotyId);
        
        $totalPrice=$credilimit['credit_limit']*$mortgage_number;//抵押总额
        $feerate=($totalPrice*0.02*$mortgage_period)/30;//利息
        
        $cond['uid']=session('uid');
        $cond['check_status']=3;
        $cond['is_mortage']=1;
        $cond['domain_typeid']=$dotyId;
        $useCount = M('UserDomains')->where($cond)->count();//总的可以抵押的域名数量
        $this->ajaxReturn(array('totalPrice'=>$totalPrice,'feerate' => sprintf("%.2f", $feerate),'useCount'=>$useCount));
        exit;
    }
    
    public function domort(){
        $authpwd=I('authpwd','');
        $transpasswd=M('user')->field('transpasswd')->find(session('uid'));
        if($transpasswd['transpasswd']!=md5($authpwd)){
            $this->ajaxReturn(array('status'=>2));
            exit;
        }
        $dotyId = I('category',0);
        $mortgage_period=I('mortgage_period',3);//抵押天数
        $mortgage_number=I('mortgage_number',1);//抵押数量
        
        $cond['uid']=session('uid');
        $cond['check_status']=3;
        $cond['is_mortage']=1;
        $cond['trans_status']=1;//正常状态下
        $cond['domain_typeid']=$dotyId;
        $dcount = M('UserDomains')->where($cond)->count();//总的可以抵押的域名数量
        if($dcount==0 || $mortgage_number>$dcount){
            $this->ajaxReturn(array('status'=>3));
            exit;
        }
        
        $credilimit = M('DomainType')->field('credit_limit')->find($dotyId);
        $totalPrice=$credilimit['credit_limit'];//抵押总额
        $feerate=($totalPrice*0.02*$mortgage_period)/30;//利息
        $feerate=sprintf("%.2f", $feerate);
        $domainList=M('UserDomains')->field('id')->where($cond)->order('id')->limit(0,$mortgage_number)->select();
        
        foreach($domainList as $dom){
            /***
             * 抵押明细user_mortdetail
             */
            $data['uid']= session('uid');
            $data['number']=1;
            $data['interest']=$feerate;
            $data['create_date']=date('y-m-d H:m:s',time());
            $data['status']=1;
            $data['credit_limit']=$credilimit['credit_limit'];
            $data['expir_day']=$mortgage_period;
            $data['expir_date']=date("Y-m-d",strtotime($mortgage_period." day")).' 00:00:00';
            $data['domaintyid']=$dotyId;
            $data['domainid']=$dom['id'];
            $mort_id = M('UserMortdetail')->add($data);
            M('UserDomains')->where('id='.$dom['id'])->setField(array('is_mortage'=>2,'entrust'=>$credilimit['credit_limit']));
        }
       
        
        /**
         * 修改域名表的数据
         */
        /**
         * 操作用户资金账户
         */
        M('UserAssets')->where('uid='.session('uid'))->setInc('avail_money',$totalPrice);
        M('UserAssets')->where('uid='.session('uid'))->setInc('notpresent_money',$totalPrice);
        
        $this->ajaxReturn(array('status'=>1));
        exit;
    }
    
    
    public function mybusines(){
        $dao=M();
        $list=$dao->query("SELECT b.id,b.name,a.busicode FROM dd_user_regbusiness a , dd_register_business b WHERE a.businesid=b.id AND a.uid=".session('uid'));
        $this->ajaxReturn($list,'JSON');
        exit();
    }
    
    
    /***
     * 发送注册短信
     */
    public function sendupdomainSms(){
        $user = D('User')->find(session('uid'));
        if($user==null || empty($user['mobile'])){
            $this->ajaxReturn(array('code'=>'error','info' => $user));//0错误，1成功
            exit;
        }
        $code = mt_rand(100000, 999999);
        $text = '您的验证码是：' . $code . '。此验证码1分钟内有效，请不要把验证码泄露给其他人。如非本人操作，可不用理会！';
        $send_time = time();
        $expire_time  = time() + 60;
        $sendObj=M('Sendkey')->find(1);
        $return = Sms::sendSms(array( 'content' => $text, 'mobile' => $user['mobile'],
            'expire_time'=>$expire_time,'extra'=>$code,'sign'=>$sendObj['sign'],'todomain'=>$sendObj['todomain'],'key'=>$sendObj['key']));
        if($return=='0' || $return==0) {
            session(array('name'=>'updomainCode','expire'=>70));
            session('updomainCode',$code);
            $this->ajaxReturn(array('code'=>'success','info' => '获取验证码成功，请及时查看！'));//0错误，1成功
        } else {
            $this->ajaxReturn(array('code'=>'error','info' => '验证码发送失败！'));//0错误，1成功
        }
    }
    
    /**
     * 申请域名转出
     */
    public function pullout(){
        
        $domain_ids=I("domain_id",'');
        $registrarId=I('registrarId','');//需要转到的域名注册商户ID
        $authpwd=I('authpwd','');
        $smscode_pullOut=I('smscode_pullOut','');
        $code = session('updomainCode');//从session里获得的验证码对象
        $sescode=strval($code);
       /*  if($smscode_pullOut!=$sescode){
            session('updomainCode',null);
            $this->ajaxReturn(array('status'=>'1'));//0错误，1成功
            exit;
        } */
        session('updomainCode',null);
        $transpasswd=M('User')->field('transpasswd')->find(session('uid'));
        if($transpasswd['transpasswd']!=md5($authpwd)){
            $this->ajaxReturn(array('status'=>'2'));
            exit;
        }
        
        $regCond['uid']=session('uid');
        $regCond['businesid']=$registrarId;
        $userBusid=M('UserRegbusiness')->field('busicode')->where($regCond)->select();
        //只有trans_status=1 ，is_mortage=1的时候此域名才可以正常被转出，其他不能被转出
        if(!empty($userBusid)){
            $data['pushid']=$userBusid[0]['busicode'];
        }
        $map['id']  = array('in',$domain_ids);
        $map['trans_status']=1;
        $map['is_mortage']=1;
        $doList = M('UserDomains')->field('id,domain')->where($map)->select();
        if(!empty($doList)) {
            $result = M('UserDomains')->where($map)->setField(array('trans_status'=>3));
            foreach ($doList as $obj) {
                $data['uid']= session('uid');
                $data['udomainid']=$obj['id'];
                $data['status']=1;
                $data['createdate']=date("Y-m-d");
                $data['regi_buines_id']=$registrarId;
                $data['domain']=$obj['domain'];
                M('DomainTurnoutlog')->add($data);
            }
        }
        $this->ajaxReturn(array('status'=>'3'));
        exit;
    }
    
    
    
}